%load alt_calib/eq_mit_mass_shock.mat
addpath /mcr/res-m1wlg01/CEtools64/   
clear
%load ../parameter_sandbox_wage_rule/eq_mit_mass.mat 
load ../free_entry/eq_mit_entry.mat
%load ../prod/calibration_v3/sandbox/eq_mit_mass



set(0,'defaultaxesfontname','cambria math') % beautify the axes a bit
set(0,'defaultTextFontName', 'cambria math')

Ns = 1 - [7.13]/100;

adj = sum(eq_mit.mu,1);
scx = pick_scale(adj, Ns);

T = 30;

figure(47)

subplot(3,3,1)
hold on
% plot(scx*(eq_mit.C - 1)); drawnow; title('C'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% 
% subplot(3,3,2)
% hold on
% plot(scx*(eq_mit.D - eq_mit.D(end))); drawnow; title('D'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% 
 Labor       =  eq_mit.l;
 total_labor =  sum(eq_mit.mu.*Labor);
% 
% subplot(3,3,3)
% hold on
% plot((scx*(total_labor - total_labor(end)))/total_labor(end)); title('Total labor demand'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% 
% subplot(3,3,4)
% hold off
% plot(scx*(eq_mit.entry_rate - eq_mit.entry_rate(end)) + eq_mit.entry_rate(end)); title('Entry rate'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% subplot(3,3,5)
% hold off
% plot(sum(eq_mit.exit.*eq_mit.mu)./sum(eq_mit.mu)); title('Exit rate'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% subplot(3,3,6)
% hold off
% plot(mass); title('Total mass'); drawnow;
% xlim([1 T]);
% 
 costs = eq_mit.l;
 prods = repmat(exp(glob.sf(:,2)), 1, options.T);
% a_prods = repmat(sim.A', glob.Nsf, 1);
% prods = prods.*a_prods;
 wages = repmat(eq_mit.W', glob.Nsf, 1);
 mkps  = eq_mit.p./(wages./prods);
 mu = mkps;
% 
 tc    = sum(wages.*costs.*eq_mit.mu);
 num    = sum(wages.*costs.*eq_mit.mu.*mkps);
% 
 mu_cw = num./tc;
% 
% 
% figure(47)
% subplot(3,3,7)
% hold off
% plot(scx*(mu_cw - mu_cw(end)) + mu_cw(end)); title('Cost-weighted markup'); xlim([1 T]); drawnow;
% 
% %
% revs = eq_mit.p.*eq_mit.y;
% 
% tr   = sum(revs.*eq_mit.mu)
% num = sum(revs.*eq_mit.mu.*mkps)/tr;
% 
% mu_sw = num./tr;
% 
% subplot(3,3,8)
% hold off
% plot((mu_sw)); title('Sales-weighted markup'); xlim([1 T]); drawnow;
% 
% 
% print('-dpng', 'figures/irfs_entry.png')


Ys    = repmat(eq_mit.C',glob.Nsf,1);
Z     = sum(eq_mit.y./Ys./prods.*eq_mit.mu);
Z     = Z.^(-1);


%% plot real output, labor demand, and nominal gdp
close all


mass = log_linear_scale_plot(adj, scx);
%mass = mass/mass(end);

T = 15;
subplot(2,3,1)
plot(mass, 'LineWidth', 4); xlim([1 T]); ylim([-8 2]) ;title('Mass of producers')
yline(0, '--')
ytickformat('percentage')


subplot(2,3,5)
plot(log_linear_scale_plot(eq_mit.C, scx), 'LineWidth', 4); xlim([1 T]); ylim(100*([.95 1]-1)); title('Output')
ytickformat('percentage')

subplot(2,3,6)
plot(log_linear_scale_plot(eq_mit.W, scx), 'LineWidth', 4); xlim([1 T]); ylim(100*([.95 1] - 1)); title('Wage')
ytickformat('percentage')

subplot(2,3,4)
plot(log_linear_scale_plot(total_labor, scx), 'LineWidth', 4); xlim([1 T]); title('Employment')
ylim([-5 0]);
ytickformat('percentage')

% subplot(2,3,4)
% ngdp = (eq_mit.entry_rate - eq_mit.entry_rate(end))*scx + eq_mit.entry_rate(end);
% plot(ngdp, 'LineWidth', 4); xlim([1 T]);; ylim([.04 .12]); title('Entry rate')

ngdp = sum(eq_mit.mu.*eq_mit.p.*eq_mit.y);

subplot(2,3,2)
labor_bill = eq_mit.W'.*sum(eq_mit.l.*eq_mit.mu);
labor_share = labor_bill./ngdp;



 plot(log_linear_scale_plot(1./labor_share, scx), 'LineWidth', 4); xlim([1 T]); title('Markup')
 yline(0, '--')

ytickformat('percentage')

%yyaxis right
%mu_cw_s = log_linear_scale(mu_cw, scx);
%plot(mu_cw_s, 'LineWidth', 4);  xlim([1 T]); drawnow;

subplot(2,3,3)
tfp = eq_mit.C'./total_labor;
%tfp = tfp(1:T);
plot(log_linear_scale_plot(tfp, scx), 'LineWidth', 4); xlim([1 T]);
title('Effective TFP')
ylim([-1 0])
ytickformat('percentage')


set(gcf,'units','points','position',[10,10,1000,600])
set(findall(gcf,'-property','FontSize'),'FontSize',16)

print('-dpng', 'figures/some_irfs_entry.png')





%% decompose difference into change in dist vs change in policy
% 
costs = eq_mit.l.*wages;

% total costs are tc

mkp_ss = mkps(:,end);
mu_ss  = eq_mit.mu(:,end);
cost_ss = costs(:,end);
T= 30
for i = 1:T
    
    mu   = eq_mit.mu(:,i);
    mkp  = mkps(:,i);
    cost = costs(:,i);
    
    
    mkp_cw(i) = sum(mkp.*mu.*cost)/sum(mu.*cost);
    
    mkp_cw_cd(i) = sum(mkp.*mu_ss.*cost_ss)/sum(mu_ss.*cost_ss);
    
    mkp_cw_cp(i) = sum(mkp_ss.*mu.*cost)/sum(mu.*cost);

    
end

%%
mkp_cw = log_linear_scale_plot(mkp_cw, scx);
mkp_cw_cd = log_linear_scale_plot(mkp_cw_cd, scx);
mkp_cw_cp = log_linear_scale_plot(mkp_cw_cp, scx);

close all
plot(mkp_cw, 'LineWidth', 4)
hold on
plot(mkp_cw_cd, 'r--', 'LineWidth', 4)
%plot(mkp_cw_cp, 'LineWidth', 4)

xlim([1 15]);

legend('Cost Weighted Markup', 'Fixed Dist')

set(findall(gcf,'-property','FontSize'),'FontSize',12)
set(gcf,'units','points','position',[10,10,500,300])
ytickformat('percentage')

print('-dpng', 'figures/entry_markup_distribution_effect.png')

%% plot entry rate, entrant share, and young share

entry_rate       = log_linear_scale(eq_mit.M./sum(eq_mit.mu,1), scx);

entr_employment = log_linear_scale(eq_mit.M.*0.52, scx)';
T = 15;
subplot(1,2,1)
plot(100*entry_rate, 'LineWidth', 4); title('Entry Rate'); xlim([1 T]); 
ytickformat('percentage')
yline(100*entry_rate(end), '--')

% add in info from BDS


subplot(1,2,2)
plot(100*entr_employment, 'LineWidth', 4); title('Emp share - entrants'); xlim([1 T]);
yline(100*entr_employment(end), '--')
ytickformat('percentage')

% add in info from BDS

%subplot(1,3,3)
%plot(exit_mass(1:T)./total_mass(1:T), 'LineWidth', 4); title('Exit rate');
%ylim([.06 .13]);

% add in info from BDS

set(gcf,'units','points','position',[10,10,750,450])
set(findall(gcf,'-property','FontSize'),'FontSize',16)

print('-dpng', 'figures/entrants.png')

